package property

import (
	"encoding/json"
	"strconv"

	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"

	// "github.com/jinzhu/gorm"

	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
)

type (
	Property struct {
		ID        bson.ObjectId `json:"_id" bson:"_id"`
		Name      string        `json:"name" bson:"name"`
		Id        string        `json:"id" bson:"id"`
		Code      string        `json:"code" bson:"code"`
		Color     string        `json:"color" bson:"color"`
		Colorcode string        `json:"colorcode" bson:"colorcode"`
		Compose   string        `json:"compose" bson:"compose"`
		Menfu     string        `json:"menfu" bson:"menfu"`
		Size      string        `json:"size" bson:"size"`
		Weight    string        `json:"weight" bson:"weight"`
		Cost      string        `json:"cost" bson:"cost"`
		Expend    string        `json:"expend" bson:"expend"`
		Provider  string        `json:"provider" bson:"provider"`
		Part      string        `json:"part" bson:"part"`
		Type      string        `json:"type" bson:"type"`           //面料类型
		Smalltype string        `json:"smalltype" bson:"smalltype"` //面料类型
		Note      string        `json:"note" bson:"note"`
		Picture   []string      `json:"picture" bson:"picture"` // 图片
		Version   string        `json:"version" bson:"version"`
	}
	InsertProperty struct {
		Name      string   `json:"name" bson:"name"`
		Id        string   `json:"id" bson:"id"`
		Code      string   `json:"code" bson:"code"`
		Color     string   `json:"color" bson:"color"`
		Colorcode string   `json:"colorcode" bson:"colorcode"`
		Compose   string   `json:"compose" bson:"compose"`
		Menfu     string   `json:"menfu" bson:"menfu"`
		Size      string   `json:"size" bson:"size"`
		Weight    string   `json:"weight" bson:"weight"`
		Cost      string   `json:"cost" bson:"cost"`
		Expend    string   `json:"expend" bson:"expend"`
		Provider  string   `json:"provider" bson:"provider"`
		Part      string   `json:"part" bson:"part"`
		Type      string   `json:"type" bson:"type"`           //面料类型
		Smalltype string   `json:"smalltype" bson:"smalltype"` //面料类型
		Note      string   `json:"note" bson:"note"`
		Picture   []string `json:"picture" bson:"picture"` // 图片
		Version   string   `json:"version" bson:"version"`
	}
)
type OXYProperty struct{}

func NewOXYProperty() *OXYProperty {
	Data := &OXYProperty{}
	return Data
}

func (ro *OXYProperty) GetPropertyListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	sort := c.QueryParam("sort")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Property"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, sort)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Property{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil

}

func (ro *OXYProperty) GetPropertyInfoHandler(c echo.Context) error {
	log.Info("GetRoleInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Property"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	p := Property{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (ro *OXYProperty) AddPropertyHandler(c echo.Context) error {
	s := &InsertProperty{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Property"
	err := model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYProperty) AddPropertysHandler(c echo.Context) error {
	s := []InsertProperty{}
	if err := c.Bind(&s); err != nil {
		log.Error("AddPropertysHandler 解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	for _, item := range s {
		model.DB().Document = "Property"
		err := model.DB().Insert(item)
		if err != nil {
			log.Error("插入出错 AddPropertysHandler", err)
		}
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYProperty) UpdatePropertyHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateSupplierTableHandler", id)

	s := &InsertProperty{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Property"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (ro *OXYProperty) DelPropertyHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSupplierTableHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Property"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
